package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.CornerPathEffect
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import com.android.kotlincoroutinemvvmdemo.R

/*
* FillTriangleView
* */
class FillTriangleView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : BaseView(context, attributeSet) {

    private val dimenValue = context.resources.getDimension(R.dimen.dp_10)

    private val mPaint0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            strokeCap = Paint.Cap.ROUND
            strokeJoin = Paint.Join.ROUND
            style = Paint.Style.FILL
            color = context.resources.getColor(R.color.color_5FD534)
        }
    }


    //CornerPathEffect 所有拐角变成圆角
    private val cornerPathEffect by lazy {
        CornerPathEffect(dimenValue)
    }

    private val mPath0 by lazy {
        Path()
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        canvas?.apply {
            mPaint0.pathEffect = cornerPathEffect
            mPath0.apply {
                moveTo(0F, 0F)
                lineTo(mWidth.toFloat() - dimenValue, mHeight / 2.0F)
                lineTo(0F, mHeight.toFloat())
                //close()  不能调用这个方法  否则的话三个角都会显示圆角
            }
            drawPath(mPath0, mPaint0)
        }
    }
}